package com.libraries.base;

import com.libraries.helper.ImgHelper;
import com.libraries.helper.Logger;
import com.libraries.listener.INetworkListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.Service;
import android.util.SparseArray;

public class BaseApplication extends Application {

	private static final String TAG = "BaseApplication";
	private static SparseArray<Object> baseArray;
	public static BaseApplication application;
	public static String cookies;
	@Override
	public void onCreate() {
		super.onCreate();
		initBaseApplication();
	}
	
	private void initBaseApplication(){
		application=this;
//		ImgHelper.initImgLoader(this);
		// 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		ImgHelper.initFrescoLoader(this);
		baseArray = new SparseArray<Object>();
	}

	public static SparseArray<Object> getBaseArray() {
		return baseArray;
	}

	/**
	 * 添加程序监听
	 * @param obj
	 */
	public static void registerListener(Object obj) {
		Logger.log(TAG, "registerListener", obj.toString());
		if (baseArray != null) {
			baseArray.put(obj.getClass().hashCode(), obj);
		}
	}
	
	/**
	 * 添加程序监听
	 * @param obj
	 */
	public static Object getListener(Object obj) {
		Logger.log(TAG, "getListener", obj.toString());
		if (baseArray != null) {
			return baseArray.get(obj.hashCode());
		}
		return null;
	}
	
	/**
	 * 根据类实列移除程序监听
	 * @param obj
	 */
	public static void unregisterListener(Object obj) {
		Logger.log(TAG, "unregisterActivity", obj);
		if (baseArray != null) {
			Object object = baseArray.get(obj.getClass().hashCode());
			baseArray.delete(obj.getClass().hashCode());
			destoryListener(object);
		}
	}
	
	/**
	 * 根据类泛列移除程序监听
	 * @param cls
	 */
	public static void unregisterListener(Class<?> cls) {
		Logger.log(TAG, "unregisterActivity", cls);
		if (baseArray != null) {
			Object object = baseArray.get(cls.hashCode());
			baseArray.delete(cls.hashCode());
			destoryListener(object);
		}
	}
	
	/**
	 * 移除所有程序监听
	 * @param keepService
	 */
	public static void unregisterAllListener(boolean keepService) {
		for (int vari = 0; vari < baseArray.size(); vari++) {
			Object object = baseArray.valueAt(vari);
			Logger.log(TAG, "unregisterAllActivity", object);
			if (keepService && object instanceof Service) {
				continue;
			}
			destoryListener(object);
		}
		baseArray.clear();
	}
	
	/**
	 * 网络状态广播
	 * @param networkType
	 * @param available
	 */
	public static void networkChanged(int networkType,boolean available) {
		for (int vari = 0; vari < baseArray.size(); vari++) {
			Object object = baseArray.valueAt(vari);
			if (object instanceof INetworkListener) {
				((INetworkListener) object).networkChanged(networkType, available);
			}
		}
	}

	@SuppressLint("NewApi")
	private static void destoryListener(Object object) {
		if (object==null) {
			return;
		}
		if (object instanceof Activity) {
			((Activity) object).finish();
		} else if (object instanceof Service) {
			((Service) object).stopSelf();
		} else if (object instanceof Fragment) {
			((Fragment) object).onDestroy();
		}
	}
	
	/**
	 * 退出app
	 * @param logout
	 */
	public static void quitApplication(boolean logout) {
		if (logout) {
			// 注销后跳转登录界面
			unregisterAllListener(false);
		} else {
			unregisterAllListener(false);
//			MobclickAgent.onKillProcess(application);//保存友盟数据
			System.exit(0);
		}
	}
	
	@Override
	public void onLowMemory() {
		super.onLowMemory();
		Logger.log(this, "onLowMemory");
		System.gc();
	}

}